package com.example.bbs.service.impl;

import ch.qos.logback.core.joran.spi.ElementSelector;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.bbs.entity.Disease;
import com.example.bbs.entity.Selfchecking;
import com.example.bbs.mapper.SelfcheckingMapper;
import com.example.bbs.service.SelfcheckingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.bbs.util.SelfcheckingResove;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiaziyun
 * @since 2021-04-30
 */
@Service
public class SelfcheckingServiceImpl extends ServiceImpl<SelfcheckingMapper, Selfchecking> implements SelfcheckingService {
    @Autowired
    private SelfcheckingMapper selfcheckingMapper;

    @Override
    public SelfcheckingResove selectById(int id) {
        Selfchecking selfchecking = selfcheckingMapper.selectById(id);
        SelfcheckingResove selfcheckingResove = new SelfcheckingResove();
        selfcheckingResove.setId(selfchecking.getId());
        selfcheckingResove.setTitle(selfchecking.getTitle());
        selfcheckingResove.setImg(selfchecking.getImg());
        selfcheckingResove.setDescription(selfchecking.getDescription());
        String[] split1 = selfchecking.getContent().split(";");
        selfcheckingResove.setCount(split1.length);
        selfcheckingResove.setContents(new SelfcheckingResove.content[selfcheckingResove.getCount()]);
        SelfcheckingResove.content[] contents = selfcheckingResove.getContents();

        for (int i = 0; i < split1.length; i++) {
            contents[i] = new SelfcheckingResove.content();
            contents[i].options = new ArrayList<>();
            String[] split2 = split1[i].split("&");
            for (int j = 0; j < split2.length; j++) {
                if (j == 0) {
                    contents[i].head = split2[j];
                } else {
                    contents[i].options.add(split2[j]);
                }
            }
        }
        return selfcheckingResove;
    }

    @Override
    public List<Selfchecking> selectPartsByIds(ArrayList<Integer> ids) {
        QueryWrapper<Selfchecking> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "title", "description");
        if ( ids.isEmpty()) {
            return selfcheckingMapper.selectList(queryWrapper);
        } else {
            queryWrapper.in("disease_id", ids);
            return selfcheckingMapper.selectList(queryWrapper);
        }
    }
}
